package project;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;

 
 

public class ImageApp 
{
	private BufferedImage loadImg = null;
	private String savePath = null;
	
	

	public void resizeImage(String userId) 
	{
		savePath = "c:/loanturtle/images/" + userId + ".jpg";
		loadImg = loadImage("C:/Users/James/Pictures/IMG000006.jpg");
		loadImg = resize(loadImg, 160, 120);
		//frame.setBounds(0, 0, loadImg.getWidth(), loadImg.getHeight());
		//JImagePanel panel = new JImagePanel(loadImg, 0, 0);
		//frame.add(panel);
		//frame.setVisible(true);
		String savePath = "c:/loanturtle/images/" + userId + ".jpg";
		saveImage(loadImg, savePath);
	}

	public static BufferedImage loadImage(String ref) 
	{
		BufferedImage bimg = null;
		try 
		{
			bimg = ImageIO.read(new File(ref));
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return bimg;
	}

	//public static void main(String[] args) 
	//{
	//	ImageApp ia = new ImageApp();
	//	JFrame frame = new JFrame("Tutorials");
	//	ia.loadAndDisplayImage(frame);
	//}
	
	public static BufferedImage resize(BufferedImage img, int newW, int newH) 
	{
		int w = img.getWidth();
		int h = img.getHeight();
		BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
		Graphics2D g = dimg.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
		g.dispose();
		return dimg;
	}
	

	/*public class JImagePanel extends JPanel
	{
		private BufferedImage image;
		int x, y;
		public JImagePanel(BufferedImage image, int x, int y) 
		{
			super();
			this.image = image;
			this.x = x;
			this.y = y;
		}
		@Override
		protected void paintComponent(Graphics g) 
		{
			super.paintComponent(g);
			g.drawImage(image, x, y, null);
		}
	}*/
	
	public static void saveImage(BufferedImage img, String ref) 
	{
		try 
		{
			String format = (ref.endsWith(".png")) ? "png" : "jpg";
			ImageIO.write(img, format, new File(ref));
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	//Gets path containing the clientId currently being used.
	public String getSavePath()
	{
		return savePath;
	}
	//Gets path for specified client.
	public String getSavePath(String clientId)
	{
		return "c:/Users/James/Pictures/IMG000006.jpg";
	}

}
	
	



 